package org.ofbiz.consolidation.order.service;

import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.consolidation.util.Classify;
import org.ofbiz.consolidation.util.CombineableOrder;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

public class OrderSearchServices {
	
    /** Service for creating a new order */
    public static Map<String, Object> findCombineableOrder(DispatchContext ctx, Map<String, ? extends Object> context) {
        Delegator delegator = ctx.getDelegator();
//        LocalDispatcher dispatcher = ctx.getDispatcher();
        
        List<GenericValue> list = null;
        try {
			list = delegator.findByAnd("CustomerOrders", 
			        null, UtilMisc.toList("orderId"));
		} catch (GenericEntityException e) {
			return ServiceUtil.returnError(e.getMessage());
		}
        
        Classify c = new Classify();
        List<CombineableOrder> combineableOrderList = c.classfy(list);
        
        /*
        if(combineableOrderList != null && combineableOrderList.size() > 0) {
        	for(CombineableOrder orders : combineableOrderList) {
        		String key = orders.getKey();
        		for(GenericValue order : orders.getOrderList()) {
        			System.out.println(key + " <====> " + order.getString("orderId"));
        		}
        	}
        }*/
        
    	Map<String, Object> result = ServiceUtil.returnSuccess();
    	result.put("combineableOrderList", combineableOrderList);
        return result;
    }

}
